package SM4;

import com.zoe.phip.infrastructure.util.StringUtil;
import org.apache.commons.codec.binary.Base64;

/**
 * Created by Administrator on 2021/9/7.
 */
public class Test {
    public static void main(String[] args){
        //加密测试入参数据
        String plainText = "<root>  <org code=\"123211G1XXX\">XXXX医院</org>  <visitor type=\"0\" code=\"adfdf\" key=\"dw1d23\"> </visitor></root>";
        //String plainText = "国";
        SM4Utils sm4 = new SM4Utils();
        //sm4.secretKey = "447E46732632EFC1C96EEE3AB15B96EB";
        ////// TODO: 随机生成guid(),为SM4的秘钥
        sm4.secretKey = StringUtil.getUUID();
        sm4.hexString = false;

        System.out.println("SM4生成随机key："+sm4.secretKey);
        System.out.println("ECB模式加密");
        String cipherText = sm4.encryptData_ECB(plainText).toLowerCase();//TODO:  这一步返回值转小写很重要
        ////// TODO:  base64 加密

        System.out.println("ECB模式加密Base64加密值："+cipherText);
        byte[] sm4Byte=cipherText.getBytes();
        String base64Sm4= Base64.encodeBase64String(sm4Byte);
        System.out.println("ECB模式加密Base64加密值："+base64Sm4);
        System.out.println("");
        ////// TODO:  base64解密
        byte[] deByte=Base64.decodeBase64(base64Sm4);
        cipherText=new String(deByte);
        String plainText2 = sm4.decryptData_ECB(cipherText);
        System.out.println("明文: " + plainText2);
        System.out.println("");
        // String qq= SMCryptoUtil.SM2Encrypt(publicKey.toLowerCase().getBytes(),plainText.getBytes());

    }
}
